package com.ptmall.app.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.ptmall.app.MainActivity;
import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.utils.DisplayUtils;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.utils.PreferenceManager;
import com.xfly.app.ui.XfLoginActivity;

import java.util.ArrayList;


public class GuideActivity extends BaseMvpActivity {


    private static final String TAG = "GuideFragment";
    //轮播图片
    ViewPager mGuideVp;
    //skip
    ImageView mGuideIvSkip;
    //point group
    LinearLayout mGuideLlPointGroup;
    //white point
    ImageView mGuideIvWhitePoint;
    //store imageView for guide
    private ArrayList<ImageView> mImageViews;
    private int[] mImageIdViews = new int[]{R.drawable.r1, R.drawable.r2, R.drawable.r3,R.drawable.r4,R.drawable.r5};

    private View mLastGuideView;

    //小圆点之间的距离
    private int mLeftMarginMax;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_guide);
    }


//    public Intent getEmptyIntent(Context context){
//        return getEmptyIntent(context,GuideFragment.class.getName());
//    }
    @Override
    public void initViews() {
        mGuideVp = (ViewPager) findViewById(R.id.guide_vp);
        mGuideIvSkip = (ImageView) findViewById(R.id.guide_iv_skip);
        mGuideLlPointGroup = (LinearLayout) findViewById(R.id.guide_ll_point_group);
        mGuideIvWhitePoint = (ImageView) findViewById(R.id.guide_iv_white_point);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //inflate the last View of guide
        mLastGuideView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_entry, null);
        mImageViews = new ArrayList<ImageView>();
        // the point width and height
        int widthdpi = DisplayUtils.dp2px(getActivity().getApplicationContext(), 10);

        for (int i = 0; i < mImageIdViews.length; i++) {
            //create point
            ImageView point = new ImageView(getContext());
            point.setBackgroundResource(R.drawable.guide_point_normal);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthdpi, widthdpi);

            //set maegin except the frite
            if (i != 0) {
                params.leftMargin = widthdpi;
            }
            point.setLayoutParams(params);
            //添加小圆点
            mGuideLlPointGroup.addView(point);

            if (i + 1 == mImageIdViews.length) {
                ImageView imageView = (ImageView) mLastGuideView.findViewById(R.id.guide_iv_last);
//                imageView.setBackgroundResource(mImageIdViews[i]);
                GlideUtil.showImg(getContext(),mImageIdViews[i],imageView);
                //do not open this The specified child already has a parent. You must call removeView() on the child's parent first.
                // mImageViews.add(imageView);
            } else {
                ImageView imageView = new ImageView(getContext());
//                imageView.setBackgroundResource(mImageIdViews[i]);
                GlideUtil.showImg(getContext(),mImageIdViews[i],imageView);
                mImageViews.add(imageView);
            }

        }//end for


    }

    @Override
    public void initEvents() {
        mGuideVp.setAdapter(new PagerAdapter() {
            //返回数据总个数
            @Override
            public int getCount() {
                return mImageIdViews.length;
            }

            //当前视图
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            //销毁视图
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            /**
             *
             * @param container
             * @param position  从0开始
             * @return
             */
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = null;
                if (position < mImageViews.size()) {
                    view = mImageViews.get(position);
                    // mGuideLlPointGroup.setVisibility(View.VISIBLE);
                    container.addView(view);
                } else {
                    //mGuideLlPointGroup.setVisibility(View.GONE);
                    view = mLastGuideView;
                    container.addView(view);
                }
                return view;
            }
        });//end PagerAdapter


        mGuideIvWhitePoint.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //默认会调用俩次，只需要一次，第一次进入就移除
                mGuideIvWhitePoint.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                //间距 = 第1个点距离左边距离 - 第0个点距离左边距离
                mLeftMarginMax = mGuideLlPointGroup.getChildAt(1).getLeft() - mGuideLlPointGroup.getChildAt(0).getLeft();
            }
        });//end

        mGuideVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /**
             * 当页面滑动回调会调用此方法
             *
             * @param position             当前页面位置
             * @param positionOffset       当前页面滑动百分比
             * @param positionOffsetPixels 滑动的像素数
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //红点移动的距离 = ViewPager页面的百分比* 间距
                //坐标 = 起始位置 + 红点移动的距离；
                int leftmagin = (int) (position * mLeftMarginMax + (positionOffset * mLeftMarginMax));
                //获取控件iv_red_point
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mGuideIvWhitePoint.getLayoutParams();
                //设置iv_red_point属性
                params.leftMargin = leftmagin;
                //绑定到控件上
                mGuideIvWhitePoint.setLayoutParams(params);

                //0,1,2,3      4-1
                if (position == mImageIdViews.length - 1) {
                    mGuideLlPointGroup.setVisibility(View.GONE);
                    mGuideIvWhitePoint.setVisibility(View.GONE);
                } else {
                    mGuideLlPointGroup.setVisibility(View.VISIBLE);
                    mGuideIvWhitePoint.setVisibility(View.VISIBLE);
                }
                switch (position) {
                    case 0: {
                        mGuideIvWhitePoint.setBackgroundResource(R.drawable.guide_point_selected);
                        break;
                    }
                    case 1: {
                        mGuideIvWhitePoint.setBackgroundResource(R.drawable.guide_point_selected_2);
                        break;
                    }
                    case 2: {
                        mGuideIvWhitePoint.setBackgroundResource(R.drawable.guide_point_selected_3);
                        break;
                    }
                }
            }

            /**
             * 页面被选中，回调此方法
             *
             * @param position 被选中的页面位置
             *                 此作用是最后一张图片显示button
             */
            @Override
            public void onPageSelected(int position) {

            }

            /**
             * 当页面滑动状态改变时候
             *
             * @param state
             */
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });//end


        mGuideIvSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goSplashFragment();
            }
        });//end

        mLastGuideView.findViewById(R.id.guide_iv_entry).setOnClickListener(v -> {
            goSplashFragment();
        });// end entery
    }

    /**
     * 跳转到引导页面
     */
    private void goSplashFragment() {

        PreferenceManager.edit(getActivity().getApplicationContext()).putBoolean(PreferenceManager.IS_FIRST, false).apply();

        startActivity(new Intent(getContext(),XfLoginActivity.class));
        finish();
    }



}
